const express = require('express')
const upload = express.Router()
const multiparty = require('multiparty')
const Upload = require('../models/upload')

// 头像
upload.post('/upload', (req, res) => {
  let form = new multiparty.Form();
  // 不能按提示
  form.uploadDir = "./upload/images";
  form.keepExtensions = true;   //是否保留后缀
  form.parse(req, function (err, fields, files) {  //其中fields表示你提交的表单数据对象，files表示你提交的文件对象
    console.log(files);
    const formData = {}
    formData.name = files.file[0].originalFilename
    formData.imgurl = files.file[0].path
    formData.size = files.file[0].size

    new Upload(formData).save((err, uploadDoc) => {
      if (err) {
        res.status(500).json({
          err_code: 500,
          message: "上传失败！" + err
        });
      } else {
        res.status(200).json({
          err_code: 0,
          message: "上传成功！",
          data:uploadDoc
        });
      }
    })


  })
})

module.exports = upload
